import Vue from 'vue'
import store from './store/index.js'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import * as echarts from 'echarts'
import {
  installCardDragger
} from 'carddragger'
import router from './router'

import {
  getRequest,
  postRequest
} from './util/requests.js'
Vue.prototype.$echarts = echarts;
Vue.prototype.$getRequest = getRequest;
Vue.prototype.$postRequest = postRequest
Vue.prototype.anchor = function(id) { //锚点
  document.querySelector(id).scrollIntoView({
    behavior: "smooth", // 定义动画过渡效果， "auto"或 "smooth" 之一。默认为 "auto"
    block: "center", // 定义垂直方向的对齐， "start", "center", "end", 或 "nearest"之一。默认为 "start"
    inline: "nearest" // 定义水平方向的对齐， "start", "center", "end", 或 "nearest"之一。默认为 "nearest"
  })
}

Vue.use(ElementUI)
Vue.use(installCardDragger)

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (store.state.user.username) {  // store.state.user.username
      next()
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App),
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
